--[[--
类名: TaskTrackController.lua
创建日期: 2015-4-22
创建人: 陈小虎
功能描述: 任务追踪控制器
修改内容：
修改日期:   
修改人:     
修改备注: 
]]

local TaskTrackView = import("..views.TaskTrackView")
local TaskTrackModel = import("..models.TaskTrackModel")

local TaskTrackController = class("TaskTrackController",function()
    return display.newNode()
end)

function TaskTrackController:ctor(uiRoot)
    self._model = TaskTrackModel.new()
    self._taskTrackView = TaskTrackView.new(uiRoot,self._model)
    self:addChild(self._taskTrackView)
    
    self:enableNodeEvents()
    self:addTaskTrackListener()
end

function TaskTrackController:getModel()
    return self._model
end

-- 获得主任务node
function TaskTrackController:getMainNode()
    return self._taskTrackView:getMainNode()
end

function TaskTrackController:getMainTaskTrackView()
    return self._taskTrackView
end

function TaskTrackController:onExit()
    EM:remove(self._taskTrackHandler)
    self._taskTrackHandler = nil
end

function TaskTrackController:addTaskTrackListener()
    self._taskTrackHandler = EM:on("onTaskTrackChange", function()
        self._taskTrackView:updateAllTaskItems(true)
    end)

    self._taskTrackView:addEventListener("onTrackItem",function(event)
        self:excuteTrackItem(event.taskConfig)
    end)

    app:addCustomEventListener("EVENT_UPDATE_TRACKLIST",function()
        self._model:refreshTasks()
        self._taskTrackView:fillTaskList()
    end,self)

    app:addCustomEventListener("EVENT_TEAM_UPDATE",function()
        self._model:refreshTasks()
        self._taskTrackView:fadeInTaskPanel()
        self._taskTrackView:fillTaskList()
    end,self)

    app:addCustomEventListener("EVENT_STOP_TRACKING",function()
        if not MapManager:isChangeMap() then
            self._taskTrackView:stopTrackingStatus()
            -- self._taskTrackView:stopNpcTrackItemAction()
        end
    end,self)

    app:addCustomEventListener("EVENT_START_TRACKING_STATUS",function()
        self._taskTrackView:startTrackingStatus()
    end,self)

    app:addCustomEventListener("EVENT_TASK_GUIDE_VISIBILITY",function(event)
        if not event._usedata then return end
        local visible = event._usedata.visible
    end,self)

    app:addCustomEventListener("EVENT_NPC_TRACK",function(event)
        if not event._usedata then return end
        local isStart = event._usedata.start
        local npcId = event._usedata.npcId
        local taskId = event._usedata.taskId
        local isHide
        if event._usedata.hide == nil then
            isHide = true
        else
            isHide = event._usedata.hide
        end

        if isStart then
            -- do nothing
        else
            -- self._taskTrackView:stopNpcTrackItemAction(isHide)
        end
    end,self)

    app:addCustomEventListener("EVENT_TRACK_STATUS",function(event)
        if not event._usedata then return end
        local taskId = event._usedata.taskId
        if taskId then
            TaskManager:setCurrentTrackTaskId(taskId)
        end
        self._taskTrackView:startTrackingStatus()
    end,self)
end

function TaskTrackController:autoFindWay(taskConfig)
    if DoEventHandler.isGather() then
        return  
    end
    if not taskConfig then return end
    if tonumber(taskConfig.traceSwitch) == 2 then return end -- 不可追踪

    local taskId = taskConfig.ID 

    -- 支线任务追踪时有引导
    if taskConfig.type == "1" and not taskConfig.scheduleString then
        app:sendMsg("GuideModel", "activateSideTaskGuide", tonumber(taskId))
        return
    end

    -- 判断追踪类型是否为追踪界面
    if tonumber(taskConfig.scheduleString) == 5 then
        local resource = GD:getSpecialResource(taskConfig.autoRun)
        if resource then
            display.resourceGotoFeature({feature = resource[1], param = resource[3]}) 
        end
        return
    end

    -- 其他追踪类型 直接查找对应的npc 
    local findNpcId = NpcManager:getWayNpcId(taskConfig)
    EM:notify("onTrackingWay",{trackNpcId = findNpcId,trackTaskId = taskId, findCall = function(isFinding,noError)
        if isFinding then
            DoEventHandler.setTracking(true)
            self._taskTrackView:fadeInTaskPanel()
        elseif not noError then
            FieldManager:characterStopMove()
            app:popMessage("cannotAutoPathfinding")
        end
    end})
end

function TaskTrackController:excuteTrackItem(taskConfig)
    if FieldManager:isNoTaskTrack() then
        return
    end
    if self.mExcuteTrackItem then
        return
    end
    self.mExcuteTrackItem = true
    local action = cc.Sequence:create(cc.DelayTime:create(0.5),cc.CallFunc:create(function()
        self.mExcuteTrackItem = false
    end))
    self:runAction(action)

    if taskConfig.featureon then
        UIHandler:handleMessage({id = tonumber(string.trim(taskConfig.featureon)),mode = "open"})
        return
    end

    local minLevel = tonumber(taskConfig.minLevel) or 0
    if tonumber(taskConfig.type) == 0 and minLevel > 0 then
        if UD:getPlayer().level < minLevel then
            local uiKey = "track_item_second"
            local node = display.newMessageBox(L("getexpfast"),function()
                UIHandler:handleMessage({id = Constant.FUNC_TYPE_CALENDAR,mode = "open"})
            end)
            UIHandler:addPopupUi(node,uiKey)
            return
        end
    end
    
    self:autoFindWay(taskConfig)
end

return TaskTrackController